package com.wanghang89.gmail.http.crawler;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.apache.log4j.Logger;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;

import com.wanghang89.gmail.http.Downloader;
import com.wanghang89.gmail.http.EncodingConstants;
import com.wanghang89.gmail.http.RequestFailException;
import com.wanghang89.gmail.util.BDCSContext;
import com.wanghang89.gmail.util.MathUtils;
import com.wanghang89.gmail.util.ChineseSpellingUtils;
import com.wanghang89.gmail.util.db.AdministraRegionDbUtils;

/**
 * 将中文转换为拼音,最多支持20个
 * @ClassName ChineseSpellCrawler
 * @Description TODO
 * @author 王航 
 * @date 2013-7-22
 */
public class ChineseSpellingCrawler implements EncodingConstants{
	
	private final static String ENTRANCE_URL = "http://crptransfer.moe.gov.tw/default.aspx";
	
	private final static String INPUT_VIEWSTATE ="__VIEWSTATE";
	private final static String INPUT_EVENTVALIDATION ="__EVENTVALIDATION";
	private final static String INPUT_TEXTBOX1 ="TextBox1";
	private final static String INPUT_IMAGEBUTTON1_X ="ImageButton1.x";//47
	private final static String INPUT_IMAGEBUTTON1_Y ="ImageButton1.y";//8
	private final static String INPUT_HDNPYTYPE ="hdnPyType";
	private final static String INPUT_HDNYNTONE ="hdnYNTone";
	
	private final static Logger LOGGER = Logger.getLogger(ChineseSpellingCrawler.class);
	
	public static String crawl(String ch)
	{
		List<NameValuePair> params = new ArrayList<NameValuePair>(7);
		params.add(0,new BasicNameValuePair(INPUT_HDNPYTYPE,""));
		params.add(1,new BasicNameValuePair(INPUT_HDNYNTONE,""));
		params.add(2,new BasicNameValuePair(INPUT_IMAGEBUTTON1_X,"47"));
		params.add(3,new BasicNameValuePair(INPUT_IMAGEBUTTON1_Y,"8"));
		params.add(4,new BasicNameValuePair(INPUT_TEXTBOX1,ch));
		do {
			Document doc = null;
			boolean isException = true;
			while (isException) {
				try {
					doc = Downloader.downloadByGet(ENTRANCE_URL,UTF_8);
					params.add(5,getViewstate(doc));
					params.add(6,getEventvalidation(doc));
					isException = false;
				} catch (RequestFailException e) {
					e.printStackTrace();
					isException = true;
					int r = MathUtils.random(30);
					try {
						Thread.sleep(r*1000);
					} catch (InterruptedException e1) {
						e1.printStackTrace();
					}
				}
			}
			doc = null;
			int r = MathUtils.random(30);
			while (r<10) {
				r = MathUtils.random(30);
			}
			try {
				Thread.sleep(r*1000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			Document rs_doc = Downloader.downloadByPost(ENTRANCE_URL,params,UTF_8,UTF_8);
			if (rs_doc==null) {
				try {
					Thread.sleep(60*1000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				continue;
			}
			Element pye = rs_doc.getElementById("Label3");
			String py = "";
			if (pye!=null) {
				py = pye.text();
			}
			params.clear();
			params = null;
			rs_doc = null;
			return py;
		} while (true);
	}
	
	private static NameValuePair getViewstate(Document doc) throws RequestFailException
	{
		Element ve = doc.getElementById("__VIEWSTATE");
		if (ve!=null) {
			return new BasicNameValuePair(INPUT_VIEWSTATE,ve.val());
		}
		throw new RequestFailException("get VIEWSTATE from doc fail!");
	}
	
	private static NameValuePair getEventvalidation(Document doc) throws RequestFailException
	{
		Element ee = doc.getElementById("__EVENTVALIDATION");
		if (ee!=null) {
			return new BasicNameValuePair(INPUT_EVENTVALIDATION,ee.val());
		}
		throw new RequestFailException("get EVENTVALIDATION from doc fail.");
	}
	
	public static void main(String[] args) {
		AdministraRegionDbUtils arDbUtils = (AdministraRegionDbUtils)BDCSContext.getBean("arDbUtils");
		int fromId = 0;
		Map<Integer,String> map = arDbUtils.getNoSpellingAdministraRegions(0);
		while (map!=null) {
			int size = map.size();
			List<Object[]> list = new ArrayList<Object[]>(size);
			Iterator<Integer> ki = map.keySet().iterator();
			while (ki.hasNext()) {
				fromId = ki.next();
				String ch = map.get(fromId);
				String fullSpelling = crawl(ch);
				String simpleSpelling = ChineseSpellingUtils.getSimpleSpelling(fullSpelling);
				list.add(new Object[]{fullSpelling,simpleSpelling,fromId});
			}
			arDbUtils.updateChineseSpelling(list);
			list.clear();
			list = null;
			map = arDbUtils.getNoSpellingAdministraRegions(fromId);
		}
	}
}
